package com.briup.run.util;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

/**
 * @version 1.0.0
 * @ClassName sendMail.java
 * @createTime 2021年06月09日 23:54:00
 */
public class MailUtil {

    /**
     * 发送邮件
     * @param emailPassword email的授权码
     * @param receiver 接收人
     * @param nickname 向接收人显示的昵称
     * @param sender 发送人
     * @param messageNumber 发送内容
     * @throws Exception
     */
    public static void sendEmails(String emailPassword, String sender, String receiver , String messageNumber, String nickname) throws Exception{

        Properties props = new Properties();

        props.setProperty("mail.smtp.auth","true");
        //传输方式
        props.setProperty("mail.transport.protocol","smtp");
        //使用的服务器网易的服务器，也可以使用QQ的
        props.setProperty("mail.smtp.host", "smtp.163.com");

        Session session = Session.getInstance(props);

        session.setDebug(true);

        Transport transport = session.getTransport();

        transport.connect(sender, emailPassword);

        Message message = createMessage(session, receiver, nickname, sender, messageNumber);

        transport.sendMessage(message, message.getAllRecipients());

        transport.close();

    }

    /**
     * 创建一封邮件
     * @param session
     * @param receiver 接收人
     * @param nickname 向接收人显示的昵称
     * @param sender 发送人
     * @param messageNumber 验证码
     * @return 返回一个邮件
     * @throws Exception
     */
    public static MimeMessage createMessage (Session session, String receiver, String nickname, String sender, String messageNumber) throws Exception{

        MimeMessage message=new MimeMessage(session);

        message.setFrom(new InternetAddress(nickname + "<" + sender + ">"));

        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiver));

        message.setSubject("邮件主题");

        message.setContent("请在客户端输入以下字符：" + messageNumber, "text/html;charset=UTF-8");

        message.setSentDate(new Date());

        return message;
    }

    /**
     * 发送邮件
     * @param receiver 接收人 (注册邮箱)
     * @param messageNumber 发送内容
     * @throws Exception
     */
    public static void sendEmails(String receiver,String messageNumber) throws Exception{

        Properties props = new Properties();

        props.setProperty("mail.smtp.auth","true");
        //传输方式
        props.setProperty("mail.transport.protocol","smtp");
        //使用的服务器网易的服务器，也可以使用QQ的
        props.setProperty("mail.smtp.host", "smtp.163.com");

        Session session = Session.getInstance(props);

        session.setDebug(true);

        Transport transport = session.getTransport();

        transport.connect("hellowsdd@163.com", "YLCFFHZUOKIYIIIF");
        String nickName = "run_community";
        Message message = createMessage(session, receiver, nickName, "hellowsdd@163.com", messageNumber);
        transport.sendMessage(message, message.getAllRecipients());

        transport.close();

    }

    public static void main(String[] args) {
        try {
            //收件人
            String receiver = "2088163042@qq.com";
            // 随机码
            String randomStr = RandomUtil.getRandomStr(8);
            sendEmails(receiver,randomStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}